
#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_errno.h"
#include "hi_pwm.h"
#include "hi_io.h"
#include "global_vars.h"
#define     STACK_SIZE             (1024)
#define     PWM_DUTY_50            (50)
#define     PWM_FREQ_4K            (4000)
#define IOT_GPIO_10  10  // Red
static int g_beepCount = 1000;  // beep test max 1000 times

#define     CLK_160M                160000000
#define     IOT_GPIO_IDX_9          9
#define     IOT_GPIO_IDX_8          8
#define     IOT_GPIO_PWM_FUNCTION   5
#define     IOT_PWM_PORT_PWM0       0
#define     IO_FUNC_GPIO_8_GPIO     0
#define     IOT_IO_PULL_UP          1
static void *PWMBeerTask(void)
{
    printf("PWMBeerTask start!\r\n");

    while (g_beepCount) {
        if (switchs) {
           IoTGpioSetOutputVal(IOT_GPIO_10, IOT_GPIO_VALUE1);
        } else {
           IoTGpioSetOutputVal(IOT_GPIO_10, IOT_GPIO_VALUE0);
        }
    }

    return NULL;
}

static void *printftest(void)
{
    while(1){
        printf("test\n");
        usleep(10000000);
        
    }
    return NULL;
}
static void StartPWMBeerTask(void)
{
    osThreadAttr_t attr1;
    osThreadAttr_t attr2;

   
    IoTGpioInit(IOT_GPIO_10);
    hi_io_set_func(IOT_GPIO_10, HI_IO_FUNC_GPIO_10_GPIO);
    IoTGpioSetDir(IOT_GPIO_10, IOT_GPIO_DIR_OUT);


    hi_io_set_func(IOT_GPIO_IDX_8, IO_FUNC_GPIO_8_GPIO);
    IoTGpioSetDir(IOT_GPIO_IDX_8, IOT_GPIO_DIR_IN);
    hi_io_set_pull(IOT_GPIO_IDX_8, IOT_IO_PULL_UP);
    // IoTGpioRegisterIsrFunc(IOT_GPIO_IDX_8, IOT_INT_TYPE_EDGE,
    //     IOT_GPIO_EDGE_FALL_LEVEL_LOW, OnButtonPressed, NULL);

    IoTWatchDogDisable();

    attr1.name = "PWMBeerTask";
    attr1.attr_bits = 0U;
    attr1.cb_mem = NULL;
    attr1.cb_size = 0U;
    attr1.stack_mem = NULL;
    attr1.stack_size = STACK_SIZE;
    attr1.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)PWMBeerTask, NULL, &attr1) == NULL) {
        printf("[StartPWMBeerTask] Falied to create PWMBeerTask!\n");
    }
    attr2.name = "printftest";
    attr2.attr_bits = 0U;
    attr2.cb_mem = NULL;
    attr2.cb_size = 0U;
    attr2.stack_mem = NULL;
    attr2.stack_size = STACK_SIZE;
    attr2.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)printftest, NULL, &attr2) == NULL) {
        printf("[StartPWMBeerTask] Falied to create printftest!\n");
    }
}
APP_FEATURE_INIT(StartPWMBeerTask);